package com.company.calendar.controller;

import com.company.calendar.dto.HolidayDTO;
import com.company.calendar.service.HolidayService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

/**
 * 节假日控制器
 * 
 * @author System
 * @since 1.0.0
 */
@RestController
@RequestMapping("/holidays")
@RequiredArgsConstructor
@Slf4j
public class HolidayController {
    
    private final HolidayService holidayService;
    
    /**
     * 创建节假日
     * 
     * @param holidayDTO 节假日DTO
     * @return 创建结果
     */
    @PostMapping
    public ResponseEntity<HolidayDTO> createHoliday(@Valid @RequestBody HolidayDTO holidayDTO) {
        log.info("创建节假日: {}", holidayDTO.getHolidayName());
        HolidayDTO result = holidayService.createHoliday(holidayDTO);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 根据ID获取节假日信息
     * 
     * @param id 节假日ID
     * @return 节假日信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<HolidayDTO> getHolidayById(@PathVariable Long id) {
        Optional<HolidayDTO> holiday = holidayService.getHolidayById(id);
        return holiday.map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    
    /**
     * 根据公司ID获取节假日列表
     * 
     * @param companyId 公司ID
     * @return 节假日列表
     */
    @GetMapping("/company/{companyId}")
    public ResponseEntity<List<HolidayDTO>> getHolidaysByCompanyId(@PathVariable Long companyId) {
        List<HolidayDTO> holidays = holidayService.getHolidaysByCompanyId(companyId);
        return ResponseEntity.ok(holidays);
    }
    
    /**
     * 根据公司ID和年份获取节假日列表
     * 
     * @param companyId 公司ID
     * @param year 年份
     * @return 节假日列表
     */
    @GetMapping("/company/{companyId}/year/{year}")
    public ResponseEntity<List<HolidayDTO>> getHolidaysByCompanyIdAndYear(@PathVariable Long companyId,
                                                                          @PathVariable Integer year) {
        List<HolidayDTO> holidays = holidayService.getHolidaysByCompanyIdAndYear(companyId, year);
        return ResponseEntity.ok(holidays);
    }
    
    /**
     * 根据公司ID和日期范围获取节假日列表
     * 
     * @param companyId 公司ID
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 节假日列表
     */
    @GetMapping("/company/{companyId}/range")
    public ResponseEntity<List<HolidayDTO>> getHolidaysByDateRange(@PathVariable Long companyId,
                                                                  @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
                                                                  @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endDate) {
        List<HolidayDTO> holidays = holidayService.getHolidaysByDateRange(companyId, startDate, endDate);
        return ResponseEntity.ok(holidays);
    }
    
    /**
     * 更新节假日信息
     * 
     * @param id 节假日ID
     * @param holidayDTO 节假日DTO
     * @return 更新结果
     */
    @PutMapping("/{id}")
    public ResponseEntity<HolidayDTO> updateHoliday(@PathVariable Long id, 
                                                   @Valid @RequestBody HolidayDTO holidayDTO) {
        log.info("更新节假日信息: {}", id);
        HolidayDTO result = holidayService.updateHoliday(id, holidayDTO);
        return ResponseEntity.ok(result);
    }
    
    /**
     * 删除节假日
     * 
     * @param id 节假日ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteHoliday(@PathVariable Long id) {
        log.info("删除节假日: {}", id);
        holidayService.deleteHoliday(id);
        return ResponseEntity.ok().build();
    }
}


